package com.fingard.dsp.bank.directbank.ynrcc01;

import com.fingard.dsp.bank.directbank.PaymentState;
import com.fingard.dsp.bank.directbank.bankInterface.ISinglePay;
import com.fingard.dsp.bank.reqrespobj.single.*;
import com.fingard.dsp.bank.reqrespobj.single.detail.RetAtsSglDetail;
import com.fingard.net.ConnState;
import com.fingard.xml.XElement;
import com.fingard.xml.XmlTextReader;
import com.fingard.xml.XmlTextWriter;

/**
 * Created by F on 2018/5/10.
 */
public class YNRCC01SinglePay extends YNRCC01Base implements ISinglePay{

    /**
     * 单笔支付
     * @param p_req1908
     * @return
     * @throws Exception
     */
    @Override
    public RetAtsSingle d_1908_SglPay(ReqSingle p_req1908) throws Exception {
        RetAtsSingle retAtsSingle = new RetAtsSingle(p_req1908.reqHeader);
        RetAtsSglDetail detail = new RetAtsSglDetail();
        try{
            //拼接请求报文头
            XmlTextWriter xmlTextWriter = new XmlTextWriter();
            YNRCC01Header header=new YNRCC01Header();
            if("1".equals(p_req1908.singleDetail.sameBnk)){
                header.transCode="B2EBankInnerTransfer";//同行
            }else if("0".equals(p_req1908.singleDetail.sameBnk)){
                header.transCode="B2ECrossBankTransfer";//它行
            }
            header.erpJnlNo= p_req1908.singleDetail.askNumber;
            wirteCommenHeader(xmlTextWriter,header);
            //拼接请求报文体
            XElement body2 = new XElement("Body","");
            XElement body = new XElement("Map","");
            body.addChild(new XElement("PayerAcNo",p_req1908.singleDetail.payAct));//转出账号
            body.addChild(new XElement("PayerAcName",p_req1908.singleDetail.payName));//转出账号户名
            body.addChild(new XElement("PayeeAcNo",p_req1908.singleDetail.recAct));//转入账号
            body.addChild(new XElement("PayeeAcName",p_req1908.singleDetail.recName));//转入账号户名
            body.addChild(new XElement("Amount",p_req1908.singleDetail.payAmount));//金额
            body.addChild(new XElement("Currency",convertCurToBank(p_req1908.singleDetail.payCur)));//币种代码
            body.addChild(new XElement("Remark",p_req1908.singleDetail.purpose));//用途
            if("0".equals(p_req1908.singleDetail.sameBnk)){
                body.addChild(new XElement("PayeeBankId",p_req1908.singleDetail.cnapsCode));//接收行行号
                body.addChild(new XElement("PayeeBankName",p_req1908.singleDetail.cnapsName));//接收行行名
                body.addChild(new XElement("UnionDeptId",p_req1908.singleDetail.cnapsCode));//接收网点号
                body.addChild(new XElement("TransferType","0"));//汇款类型
                body.addChild(new XElement("Priority",p_req1908.singleDetail.paySpd));//加急标志 1加急 0普通
            }
            nodeAnalysis(p_req1908.singleDetail.srcXElement,body,getCharset());
            body2.addChild(body);
            xmlTextWriter.writeXElement(body2);
            xmlTextWriter.writeEndDocument();
            //发送请求报文
            String[] bankResult = sendToBank(xmlTextWriter.toXmlString());

            //处理响应报文
            if(bankResult[0].length()==0){
                StringBuilder tmpSbMsg = new StringBuilder();
                XmlTextReader xmlSbReader = loadBankRespXmlReader(bankResult[1],tmpSbMsg);
                if(xmlSbReader!=null){
                    detail.respCode = "0";
                    detail.respInfo = xmlSbReader.getFirstText("/Message/Head/ReturnMsg");
                    detail.reqSeqID = p_req1908.singleDetail.askNumber;
                    /*if("000000".equals(xmlSbReader.getFirstText("/Message/Head/ReturnCode"))){
                        detail.transState = "000000";
                    }else {*/
                    detail.transState = xmlSbReader.getFirstText("/Message/Head/ReturnCode");
                    /*}*/
                    detail.payInfoCode = detail.transState;
                    detail.transAmt = "";
                }else{
                    detail.respCode ="-1";
                    detail.respInfo = tmpSbMsg.toString();
                }
            }else if (bankResult[0].equals(ConnState.FAIL)) {
                detail.respCode = "0";
                detail.respInfo = bankResult[1];
                detail.reqSeqID = p_req1908.singleDetail.askNumber;
                detail.transState = PaymentState.FAIL_Payment;
                detail.payInfoCode = bankResult[0];
            }else{
                detail.respCode ="-1";
                detail.respInfo = bankResult[1];
                detail.reqSeqID = p_req1908.singleDetail.askNumber;
            }
        }catch (Exception e){
            detail.respCode = "-1";
            detail.respInfo = e.getMessage();
            e.printStackTrace();
        }
        retAtsSingle.writeRetDetail(detail);
        return retAtsSingle;
    }

    @Override
    public RetAtsSingle d_1944_QrySglPay(ReqQrySingle p_req1944) throws Exception {
        RetAtsSingle retAtsSingle = new RetAtsSingle(p_req1944.reqHeader);
        RetAtsSglDetail detail = new RetAtsSglDetail();
        try{
            XmlTextWriter xmlTextWriter = new XmlTextWriter();
            YNRCC01Header header = new YNRCC01Header();
            header.transCode = "B2ETrsResultQry";
            header.erpJnlNo =p_req1944.reqHeader.transSeqID ;
            wirteCommenHeader(xmlTextWriter,header);
            xmlTextWriter.writeStartElement("Body");
            xmlTextWriter.writeStartElement("Map");
            xmlTextWriter.writeElementString("BeginDate", !"".equals(p_req1944.bgnDate)?p_req1944.bgnDate.substring(0,10):"");//开始时间
            xmlTextWriter.writeElementString("EndDate","");//结束时间
            xmlTextWriter.writeElementString("TransJnlNo",p_req1944.askNumber);//交易流水
            xmlTextWriter.writeElementString("PayerAcNo",p_req1944.payAct);//付款帐号
            xmlTextWriter.writeEndElement();
            xmlTextWriter.writeEndDocument();
            String[] bankResult = sendToBank(xmlTextWriter.toXmlString());

            //测试
            //WriteBankLogLn("发送至银行的报文："+xmlTextWriter.toXmlString());
            //String[] bankResult =new String[]{"","<?xml version=\"1.0\" encoding=\"utf-16\"?><Message>  <Head>    <ReturnCode>000000</ReturnCode>    <ReturnMsg>    </ReturnMsg>  </Head>  <Body>    <Map>      <TransType>1</TransType>      <JnlDate>20190117</JnlDate>      <JnlTime>15:19:00</JnlTime>\t  <PayerAcNo>05618800005460</PayerAcNo>      <Amount>11.00</Amount>      <PayeeAcNo>2222222</PayeeAcNo>\t  <PayeeAcName>对方户名</PayeeAcName>      <ReturnCode>000000</ReturnCode>      <ReturnMsg>成功</ReturnMsg>\t  <JnlState>0</JnlState>    </Map>  </Body></Message>\n"};
            //WriteBankLogStep3(bankResult[1]);

            if (bankResult[0].length() == 0) {
                StringBuilder tmpSbMsg = new StringBuilder();
                XmlTextReader xmlSbReader = loadBankRespXmlReader(bankResult[1], tmpSbMsg);
                if (xmlSbReader != null) {
                    String respCode = xmlSbReader.getFirstText("/Message/Head/ReturnCode");
                    String respInfo = xmlSbReader.getFirstText("/Message/Head/ReturnMsg");
                    if ("000000".equals(respCode)) {//交易成功
                        String code=xmlSbReader.getFirstText("/Message/Body/List/Map/ReturnCode");
                        String info=xmlSbReader.getFirstText("/Message/Body/List/Map/ReturnMsg");
                        if("000000".equals(code)){
                            detail.respCode = "0";
                            detail.respInfo = info;
                            detail.reqSeqID = p_req1944.askNumber;
                            detail.payInfoCode = xmlSbReader.getFirstText("/Message/Body/List/Map/JnlState");
                            detail.transState = detail.payInfoCode;
                        }else {
                            detail.respCode = "-1";
                            detail.respInfo = info;
                        }
                    } else {
                        detail.respCode = "-1";
                        detail.respInfo = respInfo;
                    }
                } else {
                    detail.respCode = "-1";
                    detail.respInfo = tmpSbMsg.toString();
                }
            } else {
                detail.respCode = "-1";
                detail.respInfo = bankResult[1];
            }
        }catch (Exception e){
            detail.respCode = "-1";
            detail.respInfo = e.getMessage();
            e.printStackTrace();
        }
        retAtsSingle.writeRetDetail(detail);
        return retAtsSingle;
    }

    @Override
    public RetAtsSingleBat d_1908BAT_SglBatPay(ReqSingleBat p_req1908BAT) throws Exception {
        return null;
    }

    @Override
    public RetAtsSingleBat d_1944BAT_QrySglBatPay(ReqQrySingleBat p_req1944BAT) throws Exception {
        return null;
    }
}
